package com.example.a001.part12;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.a001.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RecyclerViewActivity extends AppCompatActivity {

    private List<Flower> flowerList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);
        initFlowers();
        setupRecyclerView();
    }

    private void initFlowers() {
        String[] flowerNames = {"fl", "f2", "f3", "f4"};
        int[] flowerImages = {R.drawable.product, R.drawable.product, R.drawable.product, R.drawable.product};

        for (int i = 0; i < 12; i++) { // 生成12个元素
            Flower flower = new Flower(getRandomLengthName(flowerNames[i % 4]), flowerImages[i % 4]);
            flowerList.add(flower);
        }
    }

    private void setupRecyclerView() {
        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        FlowerAdapter adapter = new FlowerAdapter(flowerList);
        recyclerView.setAdapter(adapter);
    }

    private String getRandomLengthName(String name) {
        Random random = new Random();
        int length = random.nextInt(20) + 1;
        return name.repeat(length);
    }
}
